refactor(BA-4378): Add _observe helper to GQLMetricMiddleware#8773
Open
refactor(BA-4378): Add _observe helper to GQLMetricMiddleware#8773
Conversation
Extracted three repeated observe_request call sites in GQLMetricMiddleware.resolve() into a local _observe closure to reduce duplication. Changed `raise e` to bare `raise` for idiomatic traceback preservation. Added unit tests.
Add changelog entry documenting the extraction of the _observe helper closure in GQLMetricMiddleware.resolve() to reduce code duplication.
Remove obsolete test files for the legacy GraphQL metric middleware. These tests are no longer needed after the behavior-preserving refactoring that simplified the middleware implementation while maintaining the same functionality. Files removed: - tests/unit/manager/api/gql_legacy/BUILD - tests/unit/manager/api/gql_legacy/test_gql_metric_middleware.py
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors the legacy GraphQL metrics middleware to reduce duplication in how request metrics are recorded during resolver execution, while preserving existing runtime behavior.
Changes:
- Extracts repeated
metric_observer.observe_request(...)calls inGQLMetricMiddleware.resolve()into a local_observe(...)helper. - Switches
raise eto bareraiseto preserve original tracebacks on exceptions. - Adds a Towncrier misc fragment documenting the refactor.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/ai/backend/manager/api/gql_legacy/schema.py |
Introduces _observe closure and updates exception re-raising for better traceback preservation. |
changes/8772.misc.md |
Adds release-note fragment describing the refactor. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Refactor the _observe closure to accept a single error parameter instead of separate error_code and success kwargs. The function now uses pattern matching to derive error_code from the error type, and the try/except blocks are consolidated into a single except BaseException handler. This reduces duplication and makes the error handling flow more straightforward.
Add comprehensive unit tests for the _observe helper method in GQLMetricMiddleware, covering sync resolver timing, error handling (both BackendAIError and generic exceptions), and anonymous operation name handling for both sync and async resolvers.
da0e628 to
33e8763
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
resolves #8776 (BA-4378)
Overview
Extracts three repeated
observe_requestcall sites inGQLMetricMiddleware.resolve()into a local_observeclosure that captures the common context parameters, reducing each call site to a single line. Also changesraise eto bareraisefor idiomatic traceback preservation. This is a pure refactoring with no behavior change.Problem Statement
GQLMetricMiddleware.resolve()contained three near-identicalobserve_request(...)call sites, each passing 7 keyword argumentsraise einstead of bareraiseis non-idiomatic and can alter traceback presentationChecklist: (if applicable)
ai.backend.testdocsdirectory